
<div class="row">
    <div class="col-md-12">
        <div class="box">
            <div class="box-body">
                <form class="form-inline" name="searchForm" id="searchForm" action="" method="GET">
                    <div class="form-group">
                        <input value="{$keywords ? $keywords : '' ;}"
                               name="keywords" id="keywords" class="form-control input-sm" placeholder="客户姓名">
                    </div>

                    <div class="form-group">
                        <button class="btn btn-sm btn-primary" type="submit"><i class="fa fa-search"></i> 查询
                        </button>
                    </div>

                    <div class="form-group">
                        <button onclick="clear_form()" class="btn btn-sm btn-" type="button"><i
                                class="fa  fa-eraser"></i> 清空查询
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="box">
            {if $showDataHeader}
            <div class="box-header">
                <div>
                    {if $showDataHeaderAddButton}
                    <a title="新增" data-toggle="tooltip" class="btn btn-primary btn-sm" href="add.html">
                        新增
                    </a>
                    {/if}
                    <button id="batchPrint" class="btn btn-primary btn-sm" onclick="batchPrint()">批量打印</button>
                </div>
            </div>
            {/if}
            <div class="box-body table-responsive">
                <table class="table table-hover table-bordered datatable" width="100%">
                    <thead>
                    <tr>
                        <th>
                            <input id="data-checkall" type="checkbox" onclick="check_all(this)" class="checkbox"
                                   placeholder="全选/取消">
                        </th>
                        <!--<th>ID</th>-->
                        <th>序号</th>
                        <th>客户姓名</th>
                        <th>订单总价</th>
                        <th>订单时间</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {foreach name="list" item="item" key="index"}
                    <tr>
                        <td>
                            <input type="checkbox" onclick="check_this(this)" name="data-checkbox"
                                   data-id="{$item.id}" class="checkbox data-list-check" value="{$item.id}"
                                   placeholder="选择/取消">
                        </td>
                        <!--<td>{$item.id}</td>-->
                        <td>{$index+1}</td>
                        <td>{$item.g_user.name|default='--'}</td>
                        <td>{$item.total}</td>
                        <td>{$item.create_time|date="Y-m-d H:i:s",###}</td>
                        <td>
                            <a class="label label-{if $item.status==0}success{else/}danger{/if} AjaxButton"
                               data-toggle="tooltip" title="{if $item.status==0}点击支付{else/}点击未支付{/if}"
                               data-id="{$item.id}" data-confirm="1" data-url="disable.html">{$item.status_text}</a>
                        </td>
                        <td class="td-do">
                            <!--<a href="edit.html?id={$item.id}"
                               class="btn btn-primary btn-xs" data-toggle="tooltip" title="修改">
                                <i class="fa fa-pencil"></i>
                            </a>-->

                            <a href="order_details.html?id={$item.id}"
                               class="btn btn-primary btn-xs" data-toggle="tooltip" title="查看详情">
                                <i class="fa fa-eye"></i>
                            </a>
                            <a class="btn btn-danger btn-xs AjaxButton" data-toggle="tooltip" title="删除"
                               data-id="{$item.id}" data-url="del.html">
                                <i class="fa fa-trash"></i>
                            </a>
                        </td>
                    </tr>
                    {/foreach}
                    </tbody>
                </table>
            </div>
            {include file="template/data_footer" /}
        </div>
    </div>
</div>
<script>

    var printStr = '';
    var introduction = "{$webData['user_info']['introduction']}";
    var printhead = "{$webData['user_info']['printHead']}";


    function batchPrint() {

        dataPrintIds = [];
        $('input[name="data-checkbox"]:checked').each(function () {
            dataPrintIds.push(this.value)
        });

        if (dataPrintIds.length > 0) {
            $.ajax({
                "url": "/api/order/printList",
                "data": {
                    orderIds:dataPrintIds
                },
                "type": "post",
                async:false,
                success : function(data){
                    var jsonObj = data.data;

                    console.info(jsonObj);

                    for(var i =0;i<jsonObj.length;i++){
                        var orderJson = jsonObj[i];
                        // 如果订单有详情时生成打印内容，否则跳过
                        if(orderJson.order_details.length>0){
                            printCnt(orderJson);
                        }
                    }


                },
                error:function(data){
                    alert("新增失败");
                }
            });

            window.document.body.innerHTML = printStr;
            window.print();
            location.reload();

        } else {
            alert('请选择要打印的订单！')
        }


    };


    // 组装打印内容
    function printCnt(obj) {

        var printHead = '<div id="topContent" style="padding:0px;margin:0px;">\n' +
            '\t  <h2 style="text-align:center;padding:0px;margin:0px;">'+printhead+'</h2>' +
            '\t  <div  style="padding:0px 0 5px">' +
            '   <span style="display: inline-block;width: 33%;">客户姓名：'+obj.g_user.name+'</span><span style="display: inline-block;width: 33%;text-align: center;">电话:'+obj.g_user.mobile+'</span><span style="text-align: right;display: inline-block;width: 33%;">日期:'+new Date(obj.create_time*1000).toLocaleDateString()+'</span></div>\n' +
            '  </div>' +
            '<div><table border="1" cellspacing="0" cellpadding="1" width="100%" style="table-layout:fixed; font-size: 16px;">' +
            '<thead>\n' +
            '        <tr>\n' +
            '            <th width="30">商品名称</th>\n' +
            '            <th width="10">规格</th>\n' +
            '            <th width="10">数量</th>\n' +
            '            <th width="10">单价</th>\n' +
            '            <th width="10">金额</th>\n' +
            '            <th width="15">备注</th>\n' +
            '        </tr>\n' +
            '    </thead>\n' +
            '    <tbody>'

        var printFooter = '</tbody></table></div><div id="footerContent">\n' +
            '\t    <span style="">' +introduction+'</span></div>';


        var printCnt ='';
        // 迭代order_details  订单详情，组件表格
        for(var i =0;i< obj.order_details.length;i++){
            printCnt +='<tr>\n' +
                '            <td>'+obj.order_details[i].goods.name+'</td>\n' +
                '            <td>'+obj.order_details[i].goods.goods_spe+'</td>\n' +
                '            <td>'+obj.order_details[i].goods_num+'</td>\n' +
                '            <td>'+obj.order_details[i].price+'</td>\n' +
                '            <td>'+obj.order_details[i].subtotal+'</td>\n' +
                '            <td>'+obj.order_details[i].remark+'</td>\n' +
                '        </tr>';
        }

        printCnt +='<tr>\n' +
            '            <td>合计：</td>\n' +
            '            <td></td>\n' +
            '            <td></td>\n' +
            '            <td></td>\n' +
            '            <td>'+obj.total+'</td>\n' +
            '            <td></td>\n' +
            '        </tr>';

        printStr += '<div style="page-break-after:always">' + printHead + printCnt + printFooter +'</div>';

    }




</script>